package cn.edu.neusoft.neufooddemo.fragment;


import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import cn.edu.neusoft.neufooddemo.R;

public class CollectFragment extends Fragment {

    /*private TextView collect_shop,collect_food;*/
    private CollectShopFragment collectShopFragment;
    private CollectFoodFragment collectFoodFragment;
    private View view = null;
    protected FragmentManager childManager;
    private FragmentTransaction transaction;
    private RadioGroup rg;

      @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.tab02, container, false);
            initViews();
            initEvents();
            return view;
        }

    void initViews() {
        childManager = getChildFragmentManager();
        if (collectShopFragment == null) {
            collectShopFragment = new CollectShopFragment();
            addFragment(collectShopFragment);
        } else {
            replaceFragment(collectShopFragment);
        }
        rg = (RadioGroup) view.findViewById(R.id.radioGroup_collect);
    }

    void initEvents() {
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_collect_shop:
                        if (collectShopFragment == null) {
                            collectShopFragment = new CollectShopFragment();
                        }
                        replaceFragment(collectShopFragment);
                        break;
                    case R.id.radio_collect_food:
                        if (collectFoodFragment == null) {
                            collectFoodFragment = new CollectFoodFragment();
                        }
                        replaceFragment(collectFoodFragment);
                        break;
                }
            }
        });
    }

    protected void addFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.add(R.id.fragment_container, fragment);
        transaction.commit();
    }

    protected void replaceFragment(Fragment fragment) {
        transaction = childManager.beginTransaction();
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
    }
}
